package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @title: CategoryServiceImpl
 * @Author 椎名真白
 * @Date: 2022-04-28 10:56
 * @Version 1.0
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //构造条件选择器 查询是否和菜品有关联
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", id);
        int count = dishService.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("改分类关联的有菜品存在，不可删除");
        }

        //构造条件选择器 查询是否和套餐有关联
        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id", id);
        int count1 = setmealService.count(queryWrapper1);
        if (count1 > 0) {
            throw new CustomException("改分类关联的有套餐存在，不可删除");
        }

        //正常删除分类
        super.removeById(id);
    }
}
